package praktikumid.w02.p5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * Code from 12.09.2013 (week 2, thursday 1400).
 * Note: this code fails with cehckstyle. It's just a quick and dirty example.
 * @author Ago
 *
 */
public class Control {
	
	/**
	 * Number of loops in my example program.
	 */
	public static final int NUMBER_OF_LOOPS = 10;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("1 auto");
		System.out.println("2 autot");
		System.out.println("3 autot");
		// ..
		System.out.println("10 autot");
		
		System.out.println("-----");
		int i = 1;
		// magic number magic
		while (i <= NUMBER_OF_LOOPS) {
			if (i == 1) {
				System.out.println(i + " auto");
			} else {
				System.out.println(i + " autot");
			}
			i++;
		}
		
		for (i = 1; i <= NUMBER_OF_LOOPS; i++) {
			System.out.print(i + " auto");
			if (i != 1) {
				System.out.print("t");
			}
			System.out.println();
		}
		System.out.println("-----");
		i = 1;
		if (i < 3) {
			// hommik
			System.out.println("morning");
		} else if (i < 7) {
			System.out.println("afternoon");
		} else {
			// ohtu
			System.out.println("evening");
		}
		
		System.out.println("-----");
		switch (i) { // ==
		case 1:
		case 2:
			System.out.println("morning");
			break;
		/*case 3:
		case 4:
		case 5:
		case 6:
			System.out.println("afternoon");
			*/
		case 7:
		case 8:
		case 9:
			System.out.println("evening");
		default: // else
			System.out.println("afternoon");
			break;
		}
		
		
		System.out.println("-----");
		// array
		int[] a = new int[10];
		int[] b = new int[] {1, 2, 3, 4};
		
		b[0] = 7;
		System.out.println(b[2]);
		for (int j = 0; j < b.length; j++) {
			System.out.println(b[j]);
		}
		
		System.out.println("-----");
		// Strings
		String s1 = new String("tere");
		String s2 = "tere";
		String s3 = s2 + " tulemast\" \\ ";
		
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		
		if (s1 == s2) {
			System.out.println("s1 == s2");
		} else {
			System.out.println("s1 != s2");
		}
		
		if (s1.equalsIgnoreCase(s2)) {
			System.out.println("s1 equals s2");
		}
		System.out.println(s1.toUpperCase());
		
		for (i = 0; i < s1.length(); i++) {
			System.out.println(s1.charAt(i));
		}
		
		System.out.println(s1.indexOf("ret"));
		
		System.out.println(s3.substring(6, 7));
		
		//readInput();
		readNumbers();
	}

	
	public static void readInput() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("insert a number:");
		while (true) {
			try {
				String line = br.readLine();
				if (line.equalsIgnoreCase("exit")) {
					break;
				}
				int a = Integer.parseInt(line);
				System.out.println(a);
			} catch (NumberFormatException e) {
				System.out.println("not a number");
			} catch (IOException e) {
				System.out.println("fail");
			}
		}
	}
	
	public static void readNumbers() {
		Scanner sc = new Scanner(System.in);
		int[] history = new int[Short.MAX_VALUE]; // ~32k
		int historyCount = 0;
		while (true) {
			System.out.print("a number please:");
			if (sc.hasNext()) {
				if (sc.hasNextInt()) {
					// int
					int a = sc.nextInt();
					// let's read everything after int (line break)
					sc.nextLine();
					history[historyCount] = a;
					historyCount = historyCount + 1;
				} else {
					// not int
					String line = sc.nextLine();
				
					if (line.equalsIgnoreCase("exit")) {
						break;
					}
					// not int
					// not exit
					System.out.println("not a number");
				}
			}
		}
		
		for (int i = 0; i < historyCount; i++) {
			System.out.println(history[i]);
		}
	}
}















